package io.renren.modules.app.service.impl;
import java.util.ArrayList;
import java.util.List;
import io.renren.modules.app.dao.TZnmhAppVersionDao;
import io.renren.modules.app.entity.TZnmhAppVersion;
import io.renren.modules.app.entity.TZnmhAppVersionRole;
import io.renren.modules.app.entity.TZnmhFunctionroles;
import io.renren.modules.app.service.TZnmhAppVersionRoleService;
import io.renren.modules.app.service.TZnmhAppVersionService;
import io.renren.modules.app.service.TZnmhFunctionrolesService;
import io.renren.modules.app.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class TZnmhAppVersionServiceImpl implements TZnmhAppVersionService{
    @Autowired
    private TZnmhAppVersionDao tZnmhAppVersionDao;

    @Resource
    TZnmhFunctionrolesService tZnmhFunctionrolesService;

    @Resource
    TZnmhAppVersionRoleService tZnmhAppVersionRoleService;

    @Override
    public long getTZnmhAppVersionRowCount(){
        return tZnmhAppVersionDao.getTZnmhAppVersionRowCount();
    }
    @Override
    public List<TZnmhAppVersion> selectTZnmhAppVersion(){
        return tZnmhAppVersionDao.selectTZnmhAppVersion();
    }
    @Override
    public TZnmhAppVersion selectTZnmhAppVersionByObj(TZnmhAppVersion obj){
        return tZnmhAppVersionDao.selectTZnmhAppVersionByObj(obj);
    }
    @Override
    public TZnmhAppVersion selectTZnmhAppVersionById(String id){
        return tZnmhAppVersionDao.selectTZnmhAppVersionById(id);
    }
    @Override
    public int insertTZnmhAppVersion(TZnmhAppVersion value){
        return tZnmhAppVersionDao.insertTZnmhAppVersion(value);
    }
    @Override
    public int insertNonEmptyTZnmhAppVersion(TZnmhAppVersion value){
        return tZnmhAppVersionDao.insertNonEmptyTZnmhAppVersion(value);
    }
    @Override
    public int insertTZnmhAppVersionByBatch(List<TZnmhAppVersion> value){
        return tZnmhAppVersionDao.insertTZnmhAppVersionByBatch(value);
    }
    @Override
    public int deleteTZnmhAppVersionById(String id){
        return tZnmhAppVersionDao.deleteTZnmhAppVersionById(id);
    }
    @Override
    public int updateTZnmhAppVersionById(TZnmhAppVersion enti){
        return tZnmhAppVersionDao.updateTZnmhAppVersionById(enti);
    }
    @Override
    public int updateNonEmptyTZnmhAppVersionById(TZnmhAppVersion enti){
        return tZnmhAppVersionDao.updateNonEmptyTZnmhAppVersionById(enti);
    }

    public TZnmhAppVersionDao getTZnmhAppVersionDao() {
        return this.tZnmhAppVersionDao;
    }

    public void setTZnmhAppVersionDao(TZnmhAppVersionDao tZnmhAppVersionDao) {
        this.tZnmhAppVersionDao = tZnmhAppVersionDao;
    }

    @Override
    public List<TZnmhAppVersion> getAllTZnmhAppVersion() {
        return tZnmhAppVersionDao.getAllTZnmhAppVersion();
    }

    @Transactional
    @Override
    public List<TZnmhAppVersion> getVersion() {
        List<TZnmhAppVersion> list =tZnmhAppVersionDao.getAllTZnmhAppVersion();
        for (int i = 0;i<list.size();i++){
            TZnmhAppVersion tZnmhAppVersion = list.get(i);
            String versionName=tZnmhAppVersion.getVersionNum()+"_";
            switch (tZnmhAppVersion.getAppType()){
                case "1":
                    versionName+="我的"+"南京_";
                    break;
                case "2":
                    versionName+="我的"+"高淳_";
                    break;
                case "3":
                    versionName+="我的"+"浦口_";
                    break;
                case "4":
                    versionName+="我的"+"溧水_";
                    break;
                case "5":
                    versionName+="我的"+"江宁_";
                    break;
            }

            if(tZnmhAppVersion.getDeviceType().equals("1")){
                versionName+="android";
            }else if(tZnmhAppVersion.getDeviceType().equals("3")){
                versionName+="ios";
            }
            list.get(i).setVersionName(versionName);
            list.get(i).setCreateUser("app菜单配置员");
            list.get(i).setVersionId(RandomUtils.getId());
            tZnmhAppVersionDao.insertNonEmptyTZnmhAppVersion(list.get(i));
        }
        return list;
    }

    @Transactional
    @Override
    public List<TZnmhAppVersionRole> getAllVersionRole() {
        List<TZnmhAppVersion> list = tZnmhAppVersionDao.getAllVersionRole();
        List<TZnmhAppVersionRole> newList = new ArrayList<>();
        for (TZnmhAppVersion oldVersionRole: list) {
            String versionId="";
            String roleId="";

            //查询当此遍历的版本角色的versionId
            TZnmhAppVersion selectVersion = new TZnmhAppVersion();
            selectVersion.setAppType(oldVersionRole.getAppType());
            selectVersion.setVersionNum(oldVersionRole.getVersionNum());
            selectVersion.setDeviceType(oldVersionRole.getDeviceType());
            versionId=tZnmhAppVersionDao.selectTZnmhAppVersionByObj(selectVersion).getVersionId();

            //查询当次遍历的版本角色的RoleId
            TZnmhFunctionroles tzTRole  = new TZnmhFunctionroles();
            String[] roleName= oldVersionRole.getVersionName().split("_");
            tzTRole.setRoleName(roleName[roleName.length-1].trim());

            roleId=tZnmhFunctionrolesService.selectTZnmhFunctionrolesByObj(tzTRole).getRoleId();

            TZnmhAppVersionRole newVersionRole = new TZnmhAppVersionRole();
            newVersionRole.setVrId(oldVersionRole.getVersionId());
            newVersionRole.setRoleId(roleId);
            newVersionRole.setVersionId(versionId);
            newVersionRole.setCreateTime(oldVersionRole.getCreateTime());
            newVersionRole.setCreateUser(oldVersionRole.getCreateUser());
            newVersionRole.setUpdateTime(oldVersionRole.getUpdateTime());
            newVersionRole.setUpdateUser(oldVersionRole.getUpdateUser());
            newVersionRole.setDeleteFlag(oldVersionRole.getDeleteFlag());
            tZnmhAppVersionRoleService.insertNonEmptyTZnmhAppVersionRole(newVersionRole);
            newList.add(newVersionRole);
        }
        return newList;
    }
}